<template>
  <!-- 放置弹层组件 -->
  <!-- @open="getUserSimpleList" @open事件是弹窗打开时触发 -->
  <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @open="getUserSimpleList" @close="closeDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDeptForm" label-width="120px" :model="FormData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="FormData.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="FormData.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="FormData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in managerList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="FormData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="confirmAddDept">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUserSimpleListApi, editDepartmentApi, addDepartmentApi, getDepartmentDataiApi } from '@/api/departments'
export default {
  name: 'AddDep',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      // 复杂类型设置默认值，需要是哦用函数的方式
      default: () => {}
    },
    departList: {
      type: Array,
      default: () => []
    }
  },
  // 需要传入一个props变量来控制 显示或者隐藏
  data() {
    return {
      FormData: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: '' // 添加部门的父级部门的ID
      },
      rules: {
        name: [
          { required: true, message: '部门不能为空', trigger: 'blur' },
          { min: 1, max: 9, message: '部门名称长度为1-10', trigger: 'blur' },
          { validator: this.validateDeptName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 9, message: '部门编程长度为1-10', trigger: 'blur' },
          { validator: this.validateDeptCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }

        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍长度为1-300', trigger: 'blur' }
        ]

      },
      // 部门负责人列表
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.FormData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 获取部门详情（用于回显）
    async getDepartmentDatail() {
      const res = await getDepartmentDataiApi(this.nodeData.id)
      console.log(res)
      this.FormData = res.data
    },
    async getUserSimpleList() {
      const { data } = await getUserSimpleListApi()
      this.managerList = data
    },
    closeDialog() {
      // this.$emit('closeDialgEmit')
      this.$emit('update:showDialog', false)
      this.$refs.addDeptForm.resetFields()
      // this.FormData.pid = ''
      this.FormData = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    },
    // 校验部门名字
    validateDeptName(rule, value, callback) {
      // console.log(value)
      const children = this.nodeData.children || []
      const result = children.some(item => item.name === value)
      result ? callback(new Error('部门名称不可重复')) : callback()
    },
    // 校验部门编码
    validateDeptCode(rule, value, callback) {
      // const result = this.departList.some(item => item.code === value)
      // result ? callback(new Error('部门编码不可以重复')) : callback()
      // if (this.FormData.id && value === this.nodeData.code) {
      //   callback()
      //   return
      // } else {
      //   const flag = this.departList.some((item) => item.code === value)
      //   flag ? callback(new Error('部门编码不能重复')) : callback()
      // }
      let children = []
      if (this.FormData.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        } else {
          children = this.departList.filter(item => item.pid === this.nodeData.pid)
        }
      } else {
        children = this.nodeData.children || []
      }
      const flag = children.some(item => item.name === value)
      flag ? callback(new Error('部门名称不能重复')) : callback()
    },
    // 添加子部门
    confirmAddDept() {
      this.$refs.addDeptForm.validate(async flag => {
        if (!flag) return
        if (this.FormData.id) {
          await editDepartmentApi(this.FormData)
          this.$message.success('恭喜！修改成功')
        } else {
          this.FormData.pid = this.nodeData.id
          await addDepartmentApi(this.FormData)
          this.$message.success('恭喜!添加成功')
        }
        this.closeDialog()
        this.$parent.getDepartmenList()
      })
    }
  }

}
</script>
